package com.rrjjg.app.core.builders;

import feign.Feign;
import feign.okhttp.OkHttpClient;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.cloud.openfeign.support.SpringDecoder;
import org.springframework.cloud.openfeign.support.SpringEncoder;
import org.springframework.cloud.openfeign.support.SpringMvcContract;


/**
 * ----------------------------------------------------------------------------- <br>
 * 功能 :  <br>
 * ----------------------------------------------------------------------------- <br>
 * 作者 : Vayne <br>
 * 时间 : 2023/4/8 <br>
 */
public class FeignBuilder {

    private String url;

    private Class<?> clazz;

    private okhttp3.OkHttpClient okHttpClient;

    private ObjectFactory<HttpMessageConverters> messageConverters;

    public FeignBuilder client(ObjectFactory<HttpMessageConverters> messageConverters){
        this.messageConverters = messageConverters;
        return this;
    }

    public FeignBuilder client(okhttp3.OkHttpClient client){
        this.okHttpClient = client;
        return this;
    }

    public FeignBuilder clazz(Class<?> clazz){
        this.clazz = clazz;
        return this;
    }

    public FeignBuilder url(String url){
        this.url = url;
        return this;
    }

    public <T> T buildFeign() {
        // ObjectFactory<HttpMessageConverters> messageConverters = ()-> SpringUtil.getBean(HttpMessageConverters.class);
        return (T) Feign.builder()
                .client(new OkHttpClient(this.okHttpClient))
                .contract(new SpringMvcContract())
                .encoder(new SpringEncoder(this.messageConverters))
                .decoder(new SpringDecoder(this.messageConverters))
                .target(this.clazz, this.url);
    }
}
